package com.blog.blog.controller;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 跨域测试控制器
 */
@RestController
@RequestMapping("/api/test")
public class CorsTestController {

    /**
     * 测试GET请求
     */
    @GetMapping("/cors")
    public Map<String, Object> testCorsGet() {
        Map<String, Object> result = new HashMap<>();
        result.put("message", "CORS GET request successful");
        result.put("timestamp", System.currentTimeMillis());
        result.put("method", "GET");
        return result;
    }

    /**
     * 测试POST请求
     */
    @PostMapping("/cors")
    public Map<String, Object> testCorsPost(@RequestBody(required = false) Map<String, Object> data) {
        Map<String, Object> result = new HashMap<>();
        result.put("message", "CORS POST request successful");
        result.put("timestamp", System.currentTimeMillis());
        result.put("method", "POST");
        result.put("receivedData", data);
        return result;
    }

    /**
     * 测试PUT请求
     */
    @PutMapping("/cors")
    public Map<String, Object> testCorsPut(@RequestBody(required = false) Map<String, Object> data) {
        Map<String, Object> result = new HashMap<>();
        result.put("message", "CORS PUT request successful");
        result.put("timestamp", System.currentTimeMillis());
        result.put("method", "PUT");
        result.put("receivedData", data);
        return result;
    }

    /**
     * 测试DELETE请求
     */
    @DeleteMapping("/cors")
    public Map<String, Object> testCorsDelete() {
        Map<String, Object> result = new HashMap<>();
        result.put("message", "CORS DELETE request successful");
        result.put("timestamp", System.currentTimeMillis());
        result.put("method", "DELETE");
        return result;
    }

    /**
     * 测试带认证的请求
     */
    @GetMapping("/cors/auth")
    public Map<String, Object> testCorsAuth(@RequestHeader(value = "Authorization", required = false) String auth) {
        Map<String, Object> result = new HashMap<>();
        result.put("message", "CORS authenticated request successful");
        result.put("timestamp", System.currentTimeMillis());
        result.put("method", "GET");
        result.put("hasAuth", auth != null && !auth.isEmpty());
        return result;
    }
}
